package com.ssps.back.platform.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssps.back.platform.service.OrdersService;
import com.ssps.entity.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author li
 * @time 2020/07/22 20:41:58
 */

@Controller
@RequestMapping("orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @RequestMapping("ordersList")
    public  String ordersList(){
        return "/orders/ordersList";
    }

    @RequestMapping("ordersStatistics")
    public  String ordersStatistics(){
        return "/orders/ordersChart";
    }

    @RequestMapping("propertyordersList")
    public  String propertyordersList(){
        return "/orders/propertyordersList";
    }

    @ResponseBody
    @RequestMapping("findAll")
    public Map<String, Object> OrdersFindall(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize, String status,String uname){

        Map<String, Object> map = new HashMap<>();
        try {
            PageHelper .startPage(pageNum,pageSize);
            List<Orders> ordersList=ordersService.findAll(status,uname);
            PageInfo<Orders> pageInfo=new PageInfo<>(ordersList);
            map.put("pageInfo",pageInfo);
            map.put("ordersList",ordersList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("Arrivals")
    public Map<String,Object> ordersAway(Orders orders){
        Map<String,Object> map=new HashMap<>();

        try {
            ordersService.ordersUpdate(orders);
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",true);
            e.printStackTrace();
        }
        return map;
    }



    @ResponseBody
    @RequestMapping("Away")
    public Map<String,Object> ordersArrivals(Orders orders){
        Map<String,Object> map=new HashMap<>();
        try {
            ordersService.ordersCents(orders);
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",true);
            e.printStackTrace();
        }
        return map;
    }



    @ResponseBody
    @RequestMapping("ordersRefund")
    public Map<String,Object> ordersRefund(Orders orders){
        Map<String,Object> map=new HashMap<>();
        try {
            ordersService.ordersRefund(orders);
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",true);
            e.printStackTrace();
        }
        return map;
    }




    @ResponseBody
    @RequestMapping("ordersCount")
    public Map<String, List> guFindData(String start, String end) {
        Map<String, List> map = new HashMap<String, List>();

        //查询销售额信息
        Map<String, Double> saleDataMap = ordersService.findSaleData(start, end);

        //将map按照key排序
        saleDataMap = sortMapByKey1(saleDataMap, true);
        //用来封装日期
        ArrayList<String> xAxisList = new ArrayList<>();
        //用来封装销售额
        ArrayList<Double> seriesSaleList = new ArrayList<>();
        //遍历数据，封装到新的map中
        Set<String> keySet = saleDataMap.keySet();
        for (String key : keySet) {
            //添加日期
            xAxisList.add(key);
            //添加销售额
            seriesSaleList.add(saleDataMap.get(key));
        }
        map.put("xAxisList", xAxisList);
        map.put("seriesSaleList",seriesSaleList);
        return map;
    }
    //将map按照key进行排序
    public static Map<String, Double> sortMapByKey1(Map<String, Double> oriMap, final boolean isRise) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, Double> sortMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (isRise) {
                    // 升序排序
                    return o1.compareTo(o2);
                } else {
                    // 降序排序
                    return o2.compareTo(o1);
                }
            }
        });
        sortMap.putAll(oriMap);
        return sortMap;
    }

}
